package asynq

import (
	"log"
	"os"

	"gitee.com/huangxinbo/xcas/asynq/app"
	_ "gitee.com/huangxinbo/xcas/internal/app/docs" // Import docs package
	"github.com/spf13/cobra"
	"go.uber.org/zap"
)

var (
	StartCmd = &cobra.Command{
		Use:     "asynq",
		Short:   "Start cas async queue",
		Example: "cas asynq",
		Run: func(cmd *cobra.Command, args []string) {
			run()
		},
	}
)

func run() {
	app, err := app.NewApp()
	if err != nil {
		log.Fatalf("Failed to new app: %v", err)
	}

	if err := app.Run(); err != nil {
		app.Logger.Error("Failed to start server", zap.Error(err))
		os.Exit(1)
	}

	if err := app.Shutdown(); err != nil {
		app.Logger.Error("Server shutdown with error", zap.Error(err))
	} else {
		app.Logger.Info("Server shutdown gracefully")
	}
	app.Logger.Info("Server exit.")
}
